<?php
/**
 * 
 * @author Freezy
 *
 */
class Ranking extends MY_Controller {

	function Ranking(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function index(){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('departemen_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array('view'=>'template/horz_nav', 
											'view_data'=>array('data'=>$CONF['nav']['psdm']));
		$layoutConf['right_content'] = array('view'=>'ranking/overview', 
											'view_data'=>$CONF['search']);
		$this->initialize_layout($layoutConf);
		
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'] = array_merge(
			$pageConf['jscript_default'], 
			array('psdm/ranking','datepicker'));
		$pageConf['jscript'] = $pageConf['jscript_default'];
		
		$pageConf['css_default'] = array_merge($pageConf['css_default'], array('datepicker'));
		$pageConf['css'] = $pageConf['css_default'];
		
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
		
		//debug penilaian
//		$this->load->helper('penilaian');
//		insertPointPenilaian(1);
	}
	
	function _search(){
		$this->load->model('ranking_model');

		$key = $this->input->post('key');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		$orderby = $orderby? $orderby : "rank_point desc";
		$periode1 = $this->input->post('periode1');
		$periode2 = $this->input->post('periode2');
		
		$table_content = "";
		$row = 0;
		$data = $this->ranking_model->select_ranking($key, 
			"DATE_FORMAT(rank_periode,'%Y-%m') BETWEEN DATE_FORMAT('$periode1','%Y-%m') AND DATE_FORMAT('$periode2','%Y-%m')", 
			$limit, $offset, ($orderby? "$orderby," : "")."DATE_FORMAT(rank_periode,'%Y-%m') asc");
		$table_content = $this->load->view('ranking/table_content', array('data'=>$data),true); 	
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->ranking_model->count_record()
		));
	}

	function _drillRank($karyID,$periodeStart, $periodeEnd){
		$this->load->model(array('penilaian_model','ranking_model','karyawan_model'));
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		$conf1 = $conf['achievement']['jenis_options'];
		$conf2 = array_merge(
			$conf['detachievement']['tingkat_options']['PE'], 
			$conf['detachievement']['tingkat_options']['PR']);
		
		$karyData = $this->karyawan_model->select_by_id($karyID);
		$karyData = $karyData->row();
		$penilaianData = $this->penilaian_model->getByPeriode($karyID, $periodeStart, $periodeEnd,false,false,"point_tgl_aktif asc");
		$this->load->view('ranking/table_drill',array(
			'periodeStart'=>$periodeStart,
			'periodeEnd'=>$periodeEnd,
			'kary_id'=>$karyID,
			'kary_nama'=>$karyData->kary_nama,
			'penilaianData'=>$penilaianData,
			'jenis_options'=> $conf1,
			'tingkat_options'=> $conf2,
		));
	}
}
/* End of file psdm/ranking.php */
/* Location: ./system/application/controllers/psdm/ranking.php */